<?php
$hostName = gethostname();
if($hostName == 'web-01'){
    define('API_URL','http://item.52ywy.com/v2.1/');
}else{
    define('API_URL','http://item.52ywy.com/v2.1/');
}

class Rpc {
    
	private static $serverList = array('item','sns','user','cart');
	private static $rpcCompressor = '';//支持字符串压缩GZ
	const API_SECRETY_KEY = 'Ka48qGTlf00PSoNnZwM3Rx8PG2oOs1RK';
	
	/**
	 * 获取数据
	 * @param unknown $appName
	 * @param unknown $className
	 * @param unknown $methodName
	 * @param unknown $param
	 */
	public static function getData($appName,$class,$method,$param)
	{
		//检查参数
		$status = self::checkRequestParam($appName, $class, $method, $param);
		if(!$status)
		{
			return false;
		}
		
		//替换平台
		$paramStr = json_encode($param);
		$paramStr = str_replace('#client_platform#',3,$paramStr);
		$paramStr = str_replace('#client_system#',3,$paramStr);
		$param = json_decode($paramStr,true);
		//组装请求json
		$dataJson = self::getRequestJson($appName, $class, $method, $param);
		//socket拉取服务器数据
		$result = self::socketRequest($appName, $dataJson);
		return $result;
	}
	
	/**
	 * 检查参数是否正确
	 * @param unknown $appName
	 * @param unknown $class
	 * @param unknown $method
	 * @param unknown $param
	 * @throws RpcException
	 * @return boolean
	 */
	private static function checkRequestParam($appName,$class,$method,$param = array())
	{
		//检查参数
		if(!in_array($appName,self::$serverList))
		{
			throw new RpcException('appName not found');
			return false;
		}
		if(empty($class))
		{
			throw new RpcException('class is empty');
			return false;
		}
		if(empty($method))
		{
			throw new RpcException('method is empty');
			return false;
		}
		return true;
	}
	
	/**
	 * 获取请求json数据
	 * @param unknown $appName
	 * @param unknown $class
	 * @param unknown $method
	 * @param unknown $param
	 */
	private static function getRequestJson($appName,$class,$method,$param)
	{
		//请求数组
		$req = array(
						'data' => array(
							'user' 			=> 'haioo', // 用户名 ,必要参数
							'classname' => $class,  // 请求接口类,必要参数
							'method' 		=> $method, // 请求接口方法,必要参数
							'params' 		=> $param, // 请求接口参数,数组array('param' => 'value',...),必要参数
						),
						'signature' => ''
		);
		//计算签名
		$gather = '';
		foreach($req['data'] as $k=>$v){
			if($k == 'params'){
				$gather .= $k.'='.implode(',',$v);
			}else{
				$gather .= "{$k}={$v}";
			}
		}
		$req['signature'] = md5($gather.self::API_SECRETY_KEY);
		//请求json
		$reqJson = json_encode($req);
		return $reqJson;
	}
	
	/**
	 * socket请求服务器
	 * @param unknown $data
	 * @return 返回服务器返回结果数组
	 */
	private static function socketRequest($appName,$data)
	{
		//使用http线路
		$url = sprintf(API_URL,$appName);
		$array = array('data'=>$data);
		$header = array(
		    'CLIENT-PLATFORM:3', //ios=1,android=2,pc=3,wap=4
		    'CLIENT-APP-VERSION:',//app拿各自的app版本号
		    'MEMBER-ID:',//当前登录用户ID，没有传0
		    'MEMBER-SIGNATURE:',//登陆成功返回
		    'CLIENT-ID-CARD:',//客户端唯一识别码，等于以前的phoneid
		    'CLIENT-DEVICE-MODEL:',//客户端设备型，如iphone 5s
		    'CLIENT-SYSTEM-VERSION:'//系统版本号
		);
		$result = curl_file_get_contents($url, $array, $header);
		$result = json_decode($result, true);
		$result = (!isset($result['result']) || $result['result']==='')? array('code'=>'-3','msg'=>'sorry, API system catch exception','result'=>array()) : $result;
		return $result;
	}
}